﻿#region << 版 本 注 释 >>
/*
 * ========================================================================
 * Copyright(c) 北京天大天科科技发展有限公司, All Rights Reserved.
 * ========================================================================
 *  
 * 【当前类文件的功能】
 *  
 * CLR版本：4.0.30319.34014
 * 作者：杨朋飞  时间：2015/4/19 10:08:46
 * 文件名：IniFile
 * 版本：V1.0.0
 * 
 * 修改者：           时间：               
 * 修改说明：
 * ========================================================================
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace YPF_CommonMethod
{
    public class IniFile
    {
        /*
         * 获取启动应用程序的物理路径
         * System.Windows.Forms.Application.StartupPath + "\\Settings.ini"
         */
        public string path;
        /// <summary>
        /// 初始化。
        /// </summary>
        /// <param name="iniPath">ini文件路径</param>
        public IniFile(string iniPath)
        {
            path = iniPath;
        }

        #region API声明

        /// <summary>
        /// 写入节点
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">key</param>
        /// <param name="val">值</param>
        /// <param name="filePath">ini文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        /// <summary>
        /// 读取节点
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">key</param>
        /// <param name="def">读取失败时的默认值</param>
        /// <param name="retVal">读取的内容缓冲区，读取之后，多余的地方使用\0填充。接收方式：StringBuilder retVal</param>
        /// <param name="size">内容缓冲区的长度</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal,
            int size, string filePath);

        /// <summary>
        /// 读取节点
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">key</param>
        /// <param name="defVal">读取失败时的默认值</param>
        /// <param name="retVal">读取的内容缓冲区，读取之后，多余的地方使用\0填充。接收方式： Byte[]</param>
        /// <param name="size">内容缓冲区的长度</param>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defVal, Byte[] retVal,
            int size, string filePath);

        #endregion


        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name="section">节点名称</param>
        /// <param name="key">key</param>
        /// <param name="value">值</param>
        public void IniWriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.path);
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        public string IniReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp.ToString();
        }
        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="section">节点名</param>
        /// <param name="key">key</param>
        /// <returns></returns>
        public byte[] IniReadValues(string section, string key)
        {
            byte[] temp = new byte[255];
            int i = GetPrivateProfileString(section, key, "", temp, 255, this.path);
            return temp;

        }


        /// <summary>
        /// 删除ini文件下所有段落
        /// </summary>
        public void ClearAllSection()
        {
            IniWriteValue(null, null, null);
        }
        /// <summary>
        /// 删除ini文件下personal段落下的所有键
        /// </summary>
        /// <param name="section">节点名</param>
        public void ClearSection(string section)
        {
            IniWriteValue(section, null, null);
        }

    }

}
